package com.learn.sunday.stock.controller;

import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.learn.sunday.stock.common.DistributedLocker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/stock")
@RefreshScope
public class StockController extends DistributedLocker {


    @Value(value = "${user.age}")
    private String username;

    @Value(value = "${name}")
    private String username2;
    private static final String LOCK_NAME = "stock";
    @Value("${server.port}")
    private String port;


    @Autowired
    private ConfigService configService;



    @RequestMapping("getConfig")
    public String getNacosConfig(@RequestParam String dataId,@RequestParam String group)
    {
        log.info("user=" + username + username2);
        String config;
        try {
            config = configService.getConfig(dataId, group, 10000);
            log.info("get config is:{}",config);
            StringBuilder sb = new StringBuilder();
            sb.append("name=zhang1").append("\n");
            sb.append("name3=zhang1").append("\n");
            sb.append("name4=zhang1").append("\n");
            sb.append("name2=zhang1").append("\n");
            sb.append("age=zhang1").append("\n");
            sb.append("name222=zhang1").append("\n");
            sb.append("user.age=zhangsan").append("\n");
            boolean b = configService.publishConfig(dataId, group, sb.toString());
            if (b) {
                log.info("publish successful!");
            } else {
                log.info("publish failed!");
            }
        } catch (NacosException e) {
            log.error("get config exception:",e);
        }
        return username + username2;
    }

    /**
     * 库存新增
     * @return
     */
    @RequestMapping("/addStock")
    public String addStock(HttpServletRequest request){
        log.info("库存新增成功");
        try {
            Thread.sleep(1L);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "库存服务-库存新增成功";
    }

    /**
     * 库存扣减
     * @return
     */
    @RequestMapping("/subStock")
    public String subStock(){
//        LockWork lockWork = new LockWork(LOCK_NAME) {
//            @Override
//            public void invoke() {
//                try {
//                    log.info("开始处理业务!");
//                    Thread.sleep(1L);
//                    log.info("库存扣减成功");
//                    log.info("业务处理完成!");
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        };
//        tryLock(lockWork);


        return "库存服务-库存扣减成功 - 端口：" + port;
    }
}
